package com.recharge.web.controller;


import com.recharge.common.domain.PageDO;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.web.ObjectResponse;
import com.recharge.domain.query.BlacklistQueryDO;
import com.recharge.domain.query.RouteFilterQueryDO;
import com.recharge.domain.vo.BlacklistDO;
import com.recharge.domain.vo.RouteFilterDO;
import com.recharge.service.BlacklistService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author reagan
 * @since 2020-08-19
 */
@RestController
@RequestMapping("/blacklist")
public class BlacklistController extends BaseController {

    @Autowired
    private BlacklistService blacklistService;

    @RequiresPermissions("blacklist:add")
    @RequestMapping(value="/add.html", method = RequestMethod.POST)
    @ApiOperation(value="增加路由过滤", notes="")
    public @ResponseBody
    ObjectResponse<RouteFilterDO> add(@RequestBody BlacklistDO domain) {

        ObjectResponse<RouteFilterDO> response = new ObjectResponse<>();
        domain.setCreator(getUserName());
        ResultDO result = blacklistService.add(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("blacklist:get")
    @RequestMapping(value="/get.html", method = RequestMethod.POST)
    @ApiOperation(value="获取路由过滤", notes="")
    public @ResponseBody
    ObjectResponse<RouteFilterDO> get(@RequestBody BlacklistDO domain) {

        ObjectResponse<RouteFilterDO> response = new ObjectResponse<>();

        ResultDO result = blacklistService.get(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((RouteFilterDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequiresPermissions("blacklist:delete")
    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    @ApiOperation(value="删除路由过滤", notes="")
    public @ResponseBody
    ObjectResponse delete(@RequestBody BlacklistDO domain) {

        ObjectResponse response = new ObjectResponse();


        ResultDO result = blacklistService.remove(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("blacklist:page")
    @RequestMapping(value="/page.html", method = RequestMethod.POST)
    @ApiOperation(value="分页路由过滤", notes="")
    public @ResponseBody  ObjectResponse<PageDO> page(@RequestBody BlacklistQueryDO query) {

        ObjectResponse<PageDO> response = new ObjectResponse<>();

        ResultDO result = blacklistService.find(query);

        if(result.isSuccess()) {
            try {
                response.setData((PageDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
            }catch (Exception e){
                e.printStackTrace();
            }
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }

        return response;
    }
}
